home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / file / finfo-3.0 / finfo-3 / finfo / finfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-22  |  3.8 KB  |  195 lines

  1. #define _POSIX_SOURCE 1
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <sys/file.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <sys/sysmacros.h>
  9. #include <pwd.h>
  10. #include <grp.h>
  11. #include <time.h>
  12. #include <string.h>
  13.  
  14. /* my includes */
  15. #include "getopt.h"
  16. #include "finfo.h"
  17.  
  18. /* functions */
  19. char *name=NULL;
  20. void main(int argc, char *argv[])
  21. {
  22.     int fd=0, opt_index=0, c=0;
  23.     struct stat buf[2]={0};
  24.     struct option long_options[3] = {
  25.         {"help", 0, 0, 'h'},
  26.         {"version", 0, 0, 'v'},
  27.         {0, 0, 0, 0}
  28.     };
  29.  
  30.     name=argv[0];
  31.     fprintf(stdout,"finfo, version "VERSION"\n"
  32.         "    Copyright (c) 1996 Peter M. Jones, "EMAIL"\n"
  33.         "\n");
  34.     if(argc==1)
  35.         help(1,NULL,'h');
  36.     while((c=getopt_long(argc, argv, "hv", long_options, &opt_index))>-1) {
  37.         switch (c) {
  38.             case 'v':
  39.                 help(0,NULL,'v');
  40.                 break;
  41.             case 'h':
  42.                 help(0,NULL,'h');
  43.                 break;
  44.             default:
  45.                 help(0,NULL,'h');
  46.                 break;
  47.         }
  48.     }
  49.     if(optind<argc) {
  50.         if(lstat(argv[optind],&buf[0])<0) {
  51.             perror(argv[0]);
  52.             exit(1);
  53.         }
  54.         if((buf[0].st_mode&S_IFMT) == S_IFLNK) {
  55.             stat(argv[optind],&buf[1]);
  56.             print(buf[1]);
  57.             printf("%c",'\n');
  58.         } else {
  59.             stat(argv[optind],&buf[0]);
  60.         }
  61.         print(buf[0]);
  62.     } else {
  63.         help(1,NULL,'h');
  64.     }
  65.     exit(0);
  66. }
  67.  
  68. void print(struct stat buf)
  69. {
  70.     dev_t maj_dev=0,min_dev=0;
  71.     mode_t operm=0;
  72.     char perms[11]={0}, uname[9]={0}, gname[9]={0};
  73.     struct tm *time=NULL;
  74.  
  75.     if (buf.st_rdev) {
  76.         maj_dev=major(buf.st_rdev);
  77.         min_dev=minor(buf.st_rdev);
  78.     }
  79.  
  80.     if (buf.st_rdev)
  81.         fprintf(stdout,
  82.             "Device:            Yes.\n"
  83.             "   Major:          %d\n"
  84.             "   Minor:          %d\n",maj_dev,min_dev);
  85.     else
  86.         fprintf(stdout,
  87.             "Device:            No.\n");
  88.     fprintf(stdout, "Inode:             %ld\n",buf.st_ino);
  89.     set_perms(perms,buf);
  90.     operm=set_octal(buf.st_mode);
  91.     fprintf(stdout, "Mode:              (%s) (0%o)\n",perms, operm);
  92.     if (perms[0]=='d')
  93.         fprintf(stdout,"Subdirectories:    %d\n",buf.st_nlink);
  94.     else
  95.         fprintf(stdout,"Links to file:     %d\n",buf.st_nlink);
  96.     getnames(buf.st_uid,buf.st_gid, uname,gname);
  97.     fprintf(stdout,"Owner Name:        %s\n"
  98.         "Group Name:        %s\n",uname,gname);
  99.     fprintf(stdout,"Size:              %ld\n",(long int)buf.st_size);
  100.     time=localtime(&buf.st_atime);
  101.     fprintf(stdout,"Last accessed:     %s",asctime(time));
  102.     time=localtime(&buf.st_mtime);
  103.     fprintf(stdout,"Last modified:     %s",asctime(time));
  104.     time=localtime(&buf.st_ctime);
  105.     fprintf(stdout,"Last inode change: %s",asctime(time));
  106. }
  107.  
  108. void getnames(long int uid, long int gid, char *uname, char *gname)
  109. {
  110.     struct passwd *pw=NULL;
  111.     struct group *gpw=NULL;
  112.  
  113.     pw=getpwuid(uid);
  114.     strcpy(uname,pw->pw_name);
  115.     gpw=getgrgid(gid);
  116.     strcpy(gname,gpw->gr_name);
  117. }
  118.  
  119. void set_perms(char *perms, struct stat buf)
  120. {
  121.     char *modes[] = {"---","--x","-w-","-wx","r--","r-x","rw-","rwx"};
  122.     int i=0, j=0;
  123.  
  124.     memset(perms,0,11);
  125.     switch (buf.st_mode & S_IFMT) {
  126.         case S_IFREG:
  127.             perms[0]='-'; 
  128.             break;
  129.         case S_IFDIR:
  130.             perms[0]='d';
  131.             break;
  132.         case S_IFCHR:
  133.             perms[0]='c';
  134.             break;
  135.         case S_IFBLK:
  136.             perms[0]='b';
  137.             break;
  138.         case S_IFLNK:
  139.             perms[0]='l';
  140.             break;
  141.         case S_IFSOCK:
  142.             perms[0]='s';
  143.             break;
  144.         case S_IFIFO:
  145.             perms[0]='f';
  146.             break;
  147.         default:
  148.             perms[0]='?';
  149.             break;
  150.     }
  151.     for(i=2; i>=0; i--) {
  152.         j = (buf.st_mode >> (i*3)) & 07;
  153.         strcat(perms, modes[j]);
  154.     }
  155.     if ((buf.st_mode & S_ISUID) != 0) {
  156.         if(perms[3]=='x')
  157.             perms[3] = 's';
  158.         else
  159.             perms[3] = 'S';
  160.     }
  161.     if ((buf.st_mode & S_ISGID) != 0) {
  162.         if(perms[6]=='x')
  163.             perms[6] = 's';
  164.         else
  165.             perms[6] = 'S';
  166.     }
  167.     if ((buf.st_mode & S_ISVTX) != 0) {
  168.         if(perms[9]=='x')
  169.             perms[9] = 't';
  170.         else
  171.             perms[9] = 'T';
  172.     }
  173. }
  174.  
  175. mode_t set_octal(mode_t oct)
  176. {
  177.     oct&=(~S_IFMT);
  178.     return(oct);
  179. }
  180.  
  181. void help(int exitval, char *fname, char prob)
  182. {
  183.     FILE *fp=NULL;
  184.  
  185.     fp=fdopen(exitval+1,"w");
  186.     switch (prob) {
  187.         case 'h':
  188.             fprintf(fp,"\nusage: finfo filename\n");
  189.             break;
  190.         case 'v':
  191.             break;
  192.     }
  193.     exit(exitval);
  194. }
  195.